package com.spirng.leetcode.day10;

public class knapsack01 {
    public static void main(String[] args) {
        int[] weight = {1,3,4};
        int[] value = {15,20,30};
        int bagSize = 4;
        testWeightBagProblem(weight,value,bagSize);
        System.out.println();
    }
    public static void testWeightBagProblem(int [] weight,int []value,int bagSize){
        int dp[][]=new int[weight.length][bagSize+1];
        for (int j =weight[0]; j <= bagSize; j++) {
             dp[0][j]=value[0];
        }
        for (int i = 1; i < weight.length; i++) {
            for (int j = 1; j < bagSize; j++) {
                if (j<weight[i]) {
                    dp[i][j]=dp[i-1][j];
                }else {
                    dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]);
                }
            }
        }
        // 打印dp数组
        for (int i = 0; i < weight.length; i++) {
            for (int j = 0; j <= bagSize; j++) {
                System.out.print(dp[i][j] + "\t");
            }
            System.out.println("\n");
        }
    }
    public static void testWeightBagProblem1(int [] weight,int []value,int bagSize){
        int dp[]=new int[bagSize+1];
        for (int i = 0; i < value.length; i++) {
            for (int j =bagSize ; j >= weight[i]; j--) {
                dp[j]=Math.max(dp[j],dp[j-weight[i]]+value[i]);
                System.out.print(dp[j]+"  ");
            }
        }
    }
}
